package com.xfcy.learning.service.impl;

import com.xfcy.base.exeption.XfcyException;
import com.xfcy.base.model.RestResponse;
import com.xfcy.content.model.po.CoursePublish;
import com.xfcy.learning.feignclient.ContentServiceClient;
import com.xfcy.learning.feignclient.MediaServiceClient;
import com.xfcy.learning.model.dto.XcCourseTablesDto;
import com.xfcy.learning.service.MyCourseTableService;
import com.xfcy.learning.service.MyLearningService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 晓风残月Lx
 * @date 2023/7/8 11:18
 */
@Service
@Slf4j
public class MyLearningServiceImpl implements MyLearningService {

    @Autowired
    ContentServiceClient contentServiceClient;

    @Autowired
    MyCourseTableService myCourseTableService;

    @Autowired
    MediaServiceClient mediaServiceClient;

    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if(coursepublish==null){
            XfcyException.cast("课程信息不存在");
        }

        // 校验学习资格

        // 如果登录
        if (StringUtils.isNotEmpty(userId)) {
            XcCourseTablesDto xcCourseTablesDto = myCourseTableService.getLearningStatus(userId, courseId);

            String learnStatus = xcCourseTablesDto.getLearnStatus();
            if ("702001".equals(learnStatus)) {
                return mediaServiceClient.getPlayUrlByMediaId(mediaId);
            } else if ("702003".equals(learnStatus)) {
                RestResponse.validfail("您的选课已过期需要申请续期或重新支付");
            }
        }

        //未登录或未选课判断是否收费
        String charge = coursepublish.getCharge();
        if(charge.equals("201000")){//免费可以正常学习
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }

        return RestResponse.validfail("请购买课程后继续学习");


    }
}
